<? 
	$title="User List"; 
	$pageid="4";
	$accessid=",a10,";

if(isset($_POST['btnsubmit']))
{
	require("required/dbconnect.php");
	
	$details=$_POST['details'];
	$details_type=$_POST['details_type'];
	$is_new=0;
	$disabled_email="readonly";
	$disabled_member_card="readonly";
	
	if(empty($_POST['register_new']))
	{
		if($details_type=="email")
		{
			$chk=mysql_query("select * from os_member where email='$details'")or die(mysql_error());
			
			if(mysql_num_rows($chk)==0)
			{
				header("Location:offline_purchase_add.php?action=1");
				die();
			}
			else
			{
				$email=mysql_result($chk,0,"email");
				$member_card_number=mysql_result($chk,0,"member_card_number");
			}		
		}
		else
		{
			$chk=mysql_query("select * from os_member where member_card_number='$details'")or die(mysql_error());
			
			if(mysql_num_rows($chk)==0)
			{
				header("Location:offline_purchase_add.php?action=2");
				die();
			}
			else
			{
				$email=mysql_result($chk,0,"email");
				$member_card_number=mysql_result($chk,0,"member_card_number");
			}
		}
		
		$member_id=mysql_result($chk,0,"id");
		$msg="<div class='dw_info'>This purchase will be registered to the member below.</div>";
			
	}
	else
	{
		$is_new=1;
		$email="";
		$member_card_number="";
		$disabled_email="";
		$disabled_member_card="";
		$msg="<div class='dw_info'>This purchase will register to new member</div>";
		$member_id=0;
	}
			
	require("required/function.php");
	require("required/header.php");
	
	checkAccess("allow_branch_sales_add",1);
?>
<script src="required/js/jquery-1.4.2.js"></script>
<script src="required/js/common_javascript.js"></script>
<?
	
	
	
	$get_info=mysql_query("select * from os_contactinfo where id=1");
	
	$a=mysql_fetch_array($get_info,MYSQL_ASSOC);
	
	
?>
<script language="javascript">
				
function checkExist(field,divName,showName){
	var value = document.getElementById(field).value;

	if(value!=""){
		changeContent('required/js/checkMemberExist.php?field='+field+'&value='+value+'&showName='+showName,divName);
	}
}

function updateMemberCard(memberCardNumber){	
	document.getElementById('member_card').value=memberCardNumber;
}


function frmValidate(frm1)
{
	if(document.frm1.email.value=="")
	{
		alert("Please enter your email");
		document.frm1.email.focus();
		return false;
	}
	
	if (document.frm1.email.value!="")
	{
		var x = document.frm1.email.value;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(x)==false)
		{ 
			alert('Please enter a valid email address');
			document.frm1.email.focus();
			return false;
		}
	}
	
	if (document.frm1.receipt_number.value=="")
	{
		alert("Please enter receipt number");
		document.frm1.receipt_number.focus();
		return false;
	}
	
	if (document.frm1.amount.value=="")
	{
		alert("Please enter purchase amount");
		document.frm1.purchase_amount.focus();
		return false;
	}
		
	
}

</script>
<script language="JavaScript" type="text/javascript" src="required/js/goAjax.js"></script>
 <script type="text/javascript" src="required/js/calendarDateInput.js"></script>
<table width="100%"  cellpadding="0" cellspacing="5" height="300">
<tr>


	<td valign="top">
			
			<form action="offline_purchase_add3.php" method="post" onsubmit="return frmValidate(this)" name="frm1">
			<table width="100%">
			
				<tr>
					<td colspan="3" class="title">
				
					<strong>Create Offline Purchase</strong>
					
					</td>
				</tr>
                <tr>
                	<td colspan="3"><?= $msg ?></td>
                </tr>
				<tr valign="top">
					<td>Email</td><td width="84%">
                    <input type="text" value="<?= $email ?>" <?= $disabled_email ?> name="email" id="email" maxlength="100" size="40"  <? if($is_new==1){ ?>onBlur="checkExist('email','divEmail','Email'); return false;" <? } ?>>
                    <div id="divEmail"></div>
                    </td>
			  </tr>
				<tr valign="top">
					<td>Member Card</td><td width="84%">
                   
                    <input type="text" value="<?= $member_card_number ?>" <?= $disabled_member_card ?> name="member_card_number" maxlength="50" size="40" id="member_card_number"  <? if($is_new==1){ ?>onBlur="checkExist('member_card_number','divMemberCard','Member card number'); return false;" <? } ?>>
                    <div id="divMemberCard"></div>
                    
                    </td>
			  </tr>
              <tr>
					<td>Purchase Date</td><td width="84%"><script>DateInput('purchase_date', true, 'YYYY-MM-DD')</script>         </td>
			  </tr>
			  <tr>
					<td>Receipt Number</td><td width="84%"><input type="text" value="" name="receipt_number"  id="receipt_number" maxlength="20" size="20">
                     <div id="divReceiptNumber"></div>
                    
                    </td>
			  </tr>
				<tr>
					<td>Purchase Amount</td><td width="84%">RM <input type="text" value="" name="amount" id="amount" maxlength="10" size="10"  onkeyup="removeAlphabet(this.name)"></td>
			  </tr>
							
			
				<td width="16%">
				  <input type="hidden" value="<?= $is_new ?>" name="is_new" />
                  <input type="hidden" value="<?= $member_id ?>" name="member_id" /> 
				  <input type="submit" value="Submit" class="button" name="btnsubmit">
				</td>
			</table>
			</form>
		</td>
	</tr>
	
	
</table>
<? } require("required/footer.php"); ?>

